﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Platform.Resources;

namespace MiniFox.Platform.Applications.Web.Filters
{
    /// <summary>
    /// 
    /// </summary>
    public class ApiAuthorizationFilter : AuthorizationFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        protected override AuthorizationClient CreateAuthClient(HttpContext httpContext)
        {
            return httpContext.GetAuthClient<ApiAuthClient>();
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="filterContext"></param>
        //public void OnAuthorization(AuthorizationFilterContext filterContext)
        //{
        //    var context = filterContext.HttpContext;
        //    HttpRequest request = context.Request;
        //    HttpResponse response = context.Response;

        //    var authClient = context.GetAuthClient<ApiAuthClient>();
        //    //获取当前url
        //    string requestUrl = request.GetDisplayUrl();

        //    //例外url检查
        //    if (authClient.IsExceptionUrl(requestUrl))
        //    {
        //        return;
        //    }
        //    if (!authClient.VerifyTokenState())//验证本地token
        //    {
        //        response.Clear();
        //        filterContext.Result = new JsonResult(new Response
        //        {
        //            Code = ResErrorCode.MSG_InvalidAccessToken,
        //            Message = ResMessage.MSG_Unauthorized
        //        });
        //        return;
        //    }
        //    //给出身份到管道上下文中
        //    context.User = authClient.Principal;

        //    this.Roles = authClient.GetRoles(requestUrl);
        //}

    }
}
